import re

from django.test import TestCase
from django.template import Template, Context

class MicroformatsTest(TestCase):
    def testHCard(self):
        obj = {
            'fn': 'Brandao',
            'n': 'Jose Mario',
            'nickname': 'Marinho',
            'url': 'http://marinho.webdoisonline.com/',
            'adr': {
                'post-office-box': '',
                'extended-address': '', 
                'street-address': '', 
                'locality': '', 
                'region': '', 
                'postal-code': '', 
                'country-name': '', 
                'type': '', 
                'value': '',
                'label': '',
                },
            'geo': {
                'latitude': '', 
                'longitude': '',
                },
            'photo': '',
            'logo': '', 
            'sound': '', 
            'bday': '',
            'title': '',
            'role': '',
            'org': {
                'organization-name': '',
                'organization-unit': '',
                },
            'category': '',
            'note': '',
            'class': '',
            'key': '', 
            'mailer': '', 
            'uid': '', 
            'rev': '',
        }

        t = Template("{% load microformats %}{% hcard object %}")
        c = Context({'object': obj})

        result = t.render(c)

        # Generic regular expression
        exp = r'<.*? class=["\']([\w_-]+ )?%s( [\w_-]+)?["\']>(.*?)</[a-zA-Z]+>'
        
        # Classes to test
        for cls in ['fn', 'n', 'nickname', 'url']:
            exp_cls = exp % cls
            f = re.findall(exp_cls, result, re.I | re.M)

            self.assertTrue(f)

